package com.pm.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.pm.business.domain.*;
import com.pm.business.service.*;
import com.pm.common.utils.DictUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.common.utils.poi.ExcelUtil;
import com.pm.common.core.page.TableDataInfo;

/**
 * 基本信息Controller
 * 
 * @author zxl
 * @date 2022-05-07
 */
@RestController
@RequestMapping("/business/compete_enterprise")
public class CompeteEnterpriseController extends BaseController
{
    @Autowired
    private ICompeteEnterpriseService competeEnterpriseService;
    @Autowired
    private ICompeteEnterpriseHistoryProjectService competeEnterpriseHistoryProjectService;
    @Autowired
    private ICompeteEnterpriseBadBehaviorService competeEnterpriseBadBehaviorService;
    @Autowired
    private ICompeteEnterpriseCertificateService competeEnterpriseCertificateService;
    @Autowired
    private ICompeteEnterpriseGradeService competeEnterpriseGradeService;

    /**
     * 查询基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompeteEnterprise competeEnterprise)
    {
        startPage();
        List<CompeteEnterprise> list = competeEnterpriseService.selectCompeteEnterpriseList(competeEnterprise);
        return getDataTable(list);
    }
    @GetMapping("/optionList")
    public TableDataInfo optionList(CompeteEnterprise competeEnterprise)
    {
        List<CompeteEnterprise> list = competeEnterpriseService.selectCompeteEnterpriseList(competeEnterprise);
        return getDataTable(list);
    }

    /**
     * 导出基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:export')")
    @Log(title = "基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CompeteEnterprise competeEnterprise)
    {
        List<CompeteEnterprise> list = competeEnterpriseService.selectCompeteEnterpriseList(competeEnterprise);
        ExcelUtil<CompeteEnterprise> util = new ExcelUtil<CompeteEnterprise>(CompeteEnterprise.class);
        util.exportExcel(response, list, "基本信息数据");
    }

    /**
     * 获取基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:query')")
    @GetMapping(value = "/{enterpriseId}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") Long enterpriseId)
    {
        return AjaxResult.success(competeEnterpriseService.selectCompeteEnterpriseByEnterpriseId(enterpriseId));
    }

    /**
     * 获取基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:query')")
    @GetMapping(value = "/appInfo/{enterpriseId}")
    public AjaxResult getAppInfo(@PathVariable("enterpriseId") Long enterpriseId)
    {
        CompeteEnterprise competeEnterprise = competeEnterpriseService.selectCompeteEnterpriseByEnterpriseId(enterpriseId);
        String legalPersonStatus = DictUtils.getDictLabel("compete_unit_legal_person_status",competeEnterprise.getLegalPersonStatus());
        competeEnterprise.setLegalPersonStatus(legalPersonStatus);

        CompeteEnterpriseHistoryProject hpCondition = new CompeteEnterpriseHistoryProject();
        hpCondition.setEnterpriseId(enterpriseId);
        List<CompeteEnterpriseHistoryProject> hpList = competeEnterpriseHistoryProjectService.selectCompeteEnterpriseHistoryProjectList(hpCondition);
        competeEnterprise.setHistoryProjectList(hpList);

        CompeteEnterpriseGrade gradeCondition = new CompeteEnterpriseGrade();
        gradeCondition.setEnterpriseId(enterpriseId);
        List<CompeteEnterpriseGrade> gradeList = competeEnterpriseGradeService.selectCompeteEnterpriseGradeList(gradeCondition);
        competeEnterprise.setGradeList(gradeList);


        CompeteEnterpriseBadBehavior bbCondition = new CompeteEnterpriseBadBehavior();
        bbCondition.setEnterpriseId(enterpriseId);
        List<CompeteEnterpriseBadBehavior> bbList = competeEnterpriseBadBehaviorService.selectCompeteEnterpriseBadBehaviorList(bbCondition);
        competeEnterprise.setBadBehaviorList(bbList);

        CompeteEnterpriseCertificate ccCondition = new CompeteEnterpriseCertificate();
        ccCondition.setEnterpriseId(enterpriseId);
        List<CompeteEnterpriseCertificate>ccList = competeEnterpriseCertificateService.selectCompeteEnterpriseCertificateList(ccCondition);
        if(ccList!=null){
            ccList.forEach(cc->{
                cc.setCertificateType(DictUtils.getDictLabel("pm_compete_qualit_category",cc.getCertificateType()));
                cc.setCertificateGrade(DictUtils.getDictLabel("pm_award_grade",cc.getCertificateGrade()));
            });
        }

        competeEnterprise.setCertificateList(ccList);


        return AjaxResult.success(competeEnterprise);
    }

    /**
     * 新增基本信息
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:add')")
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CompeteEnterprise competeEnterprise)
    {
        return toAjax(competeEnterpriseService.insertCompeteEnterprise(competeEnterprise));
    }

    /**
     * 修改基本信息
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:edit')")
    @Log(title = "基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CompeteEnterprise competeEnterprise)
    {
        return toAjax(competeEnterpriseService.updateCompeteEnterprise(competeEnterprise));
    }

    /**
     * 删除基本信息
     */
    @PreAuthorize("@ss.hasPermi('business:compete_enterprise:remove')")
    @Log(title = "基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable Long[] enterpriseIds)
    {
        return toAjax(competeEnterpriseService.deleteCompeteEnterpriseByEnterpriseIds(enterpriseIds));
    }
}
